package biolabs.util;

import java.util.Collection;
import java.util.Map;


public class Pair<A,B> {

	public A a;
	public B b;

	public Pair(A a, B b){
		this.a = a;
		this.b = b;
	}


	private static class AExtractor<A,B> implements Transformation<Pair<A,B>, A> {
		@Override
		public A apply(Pair<A, B> p) {
			return p.a;
		}
	}

	private static class BExtractor<A,B> implements Transformation<Pair<A,B>, B> {
		@Override
		public B apply(Pair<A, B> p) {
			return p.b;
		}
	}

	private static class EntryConvertor<A,B> implements Transformation<Map.Entry<A,B>, Pair<A,B>> {
		@Override
		public Pair<A,B> apply(Map.Entry<A, B> e) {
			return new Pair<A,B>(e.getKey(), e.getValue());
		}
	}


	public static <A,B> Iterable<A> asA(Iterable<Pair<A,B>> coll){
		return new TransformingIterable<Pair<A,B>, A>(coll, new AExtractor<A,B>());
	}

	public static <A,B> Collection<A> asA(Collection<Pair<A,B>> coll){
		return new TransformingCollection<Pair<A,B>, A>(coll, new AExtractor<A,B>());
	}

	public static <A,B> Iterable<B> asB(Iterable<Pair<A,B>> coll){
		return new TransformingIterable<Pair<A,B>, B>(coll, new BExtractor<A,B>());
	}

	public static <A,B> Collection<B> asB(Collection<Pair<A,B>> coll){
		return new TransformingCollection<Pair<A,B>, B>(coll, new BExtractor<A,B>());
	}

	public static <A,B> Iterable<Pair<A,B>> entriesToPairs(Iterable<Map.Entry<A, B>> coll) {
		return new TransformingIterable<Map.Entry<A,B>, Pair<A,B>>(coll, new EntryConvertor<A,B>());
	}

	public static <A,B> Collection<Pair<A,B>> entriesToPairs(Collection<Map.Entry<A, B>> coll) {
		return new TransformingCollection<Map.Entry<A,B>, Pair<A,B>>(coll, new EntryConvertor<A,B>());
	}
}